home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / MultiAnimation / skin.vsh < prev    next >
Encoding:
Text File  |  2004-09-27  |  2.9 KB  |  87 lines

  1. //
  2. // HLSL function for skinning a mesh.  In your shader, you can #define 
  3. // MATRIX_PALETTE_SIZE if desired, and then #include this file.
  4. // Copyright (c) 2000-2003 Microsoft Corporation. All rights reserved.
  5. //
  6.  
  7.  
  8. #ifndef VS_SKIN_VSH
  9. #define VS_SKIN_VSH
  10.  
  11.  
  12. //----------------------------------------------------------------------------
  13. // Global parameters 
  14. //----------------------------------------------------------------------------
  15.  
  16.  
  17. // Declare the 4x3 matrix palette.  This is the array of bone matrices used in
  18. // skinning vertices.
  19.  
  20. // The palette size is 26 by default.  This is sufficiently small for most 
  21. // vs_1_1 shaders.  Shaders targeted at vs_2_0 and above can set this higher
  22. // to accommondate more bones in a call.  For example, tiny_anim.x has 35
  23. // bones, and so can be rendered in a single call if MATRIX_PALETTE_SIZE is
  24. // set to 35 or more.
  25.  
  26. // An HLSL shader can set MATRIX_PALETTE_SIZE_DEFAULT to a different value.
  27. // The calling app can also set it in the D3DXMACRO structure when compiling
  28. // the shader.  The calling app can query the actual palette size by examining
  29. // MATRIX_PALETTE_SIZE (but changing it after compilation will not change the
  30. // palette size in the compiled shader, of course).
  31.  
  32.  
  33. #ifndef MATRIX_PALETTE_SIZE_DEFAULT
  34. #define MATRIX_PALETTE_SIZE_DEFAULT 26
  35. #endif
  36.  
  37. const int MATRIX_PALETTE_SIZE = MATRIX_PALETTE_SIZE_DEFAULT;
  38. float4x3 amPalette[ MATRIX_PALETTE_SIZE_DEFAULT ];
  39.  
  40.  
  41. //----------------------------------------------------------------------------
  42. // Shader body - VS_ Skin
  43. //----------------------------------------------------------------------------
  44.  
  45. // define the inputs -- caller must fill this, usually right from the VB
  46. struct VS_SKIN_INPUT
  47. {
  48.     float4      vPos;
  49.     float3      vBlendWeights;
  50.     float4      vBlendIndices;
  51.     float3      vNor;
  52. };
  53.  
  54. // return skinned position and normal
  55. struct VS_SKIN_OUTPUT
  56. {
  57.     float4 vPos;
  58.     float3 vNor;
  59. };
  60.  
  61. // call this function to skin VB position and normal
  62. VS_SKIN_OUTPUT VS_Skin( const VS_SKIN_INPUT vInput, int iNumBones )
  63. {
  64.     VS_SKIN_OUTPUT vOutput = (VS_SKIN_OUTPUT) 0;
  65.  
  66.     float fLastWeight = 1.0;
  67.     float fWeight;
  68.     float afBlendWeights[ 3 ] = (float[ 3 ]) vInput.vBlendWeights;
  69.     int aiIndices[ 4 ] = (int[ 4 ]) D3DCOLORtoUBYTE4( vInput.vBlendIndices );
  70.     
  71.     for( int iBone = 0; (iBone < 3) && (iBone < iNumBones - 1); ++ iBone )
  72.     {
  73.         fWeight = afBlendWeights[ iBone ];
  74.         fLastWeight -= fWeight;
  75.         vOutput.vPos.xyz += mul( vInput.vPos, amPalette[ aiIndices[ iBone ] ] ) * fWeight;
  76.         vOutput.vNor     += mul( vInput.vNor, amPalette[ aiIndices[ iBone ] ] ) * fWeight;
  77.     }
  78.     
  79.     vOutput.vPos.xyz += mul( vInput.vPos, amPalette[ aiIndices[ iNumBones - 1 ] ] ) * fLastWeight;
  80.     vOutput.vNor     += mul( vInput.vNor, amPalette[ aiIndices[ iNumBones - 1 ] ] ) * fLastWeight;
  81.  
  82.     return vOutput;
  83. }
  84.  
  85.  
  86. #endif // #ifndef VS_SKIN_VSH
  87.